package com.lxt.blog.controller;

import com.lxt.blog.dao.ArticleDao;
import com.lxt.blog.dao.ArticleDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 文章控制器
 */
@WebServlet("/article")
public class ArticleController extends HttpServlet {

    protected ArticleDao articleDao= new ArticleDaoImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String key = request.getParameter("key");
        if ("pageSize".equals(key)){
            //文章总数
            pageSize(request,response);
        }
    }

    /**
     * 查询文章总数
     * 可选参数user 为T表示查找当前用户的文章总数，为空则查询所有文章
     * /blog/article?key=pageSize&user=T
     * @param request
     * @param response
     */
    private void pageSize(HttpServletRequest request,HttpServletResponse response) {
        String name = request.getParameter("user") == "T" ? (String)request.getSession().getAttribute("username") : "NULL";
        int pageSize = articleDao.selectArticleSize(name);
        try {
            response.getWriter().println(pageSize);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
